package org.cuadroanotacionapi.persistencia;

import org.cuadroanotacionapi.beans.Jos_fastball_division;
import org.cuadroanotacionapi.beans.*;
import org.cuadroanotacionapi.controller.*;
import java.io.Serializable;
import java.util.List;
import java.util.ArrayList;
import java.util.*;
import java.sql.Connection;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.PreparedStatement;
import java.util.Map;
import java.util.Iterator;
import java.util.HashMap;
import java.util.Map;
 import javax.sql.DataSource;
import org.cuadroanotacionapi.generales.Mensajeria;
import org.cuadroanotacionapi.generales.Sesion;


/**
*
* @author
*/

public class Jos_fastball_divisionPersistencia implements Serializable {
private static final long serialVersionUID = 1L;
public static Exception excepcion;
public static boolean esMensaje = false;
public static Connection con = null;

public Jos_fastball_divisionPersistencia(){
         esMensaje =false;
}


/**
*
* Insertar()
*/

public static boolean Insertar (Jos_fastball_division jos_fastball_division ) {
  excepcion = null;
  try {
      esMensaje =false;
   
   
   
     if (!Conectar()) {
         return false;
      }
      PreparedStatement pinsert = null;
      String sql = "insert into jos_fastball_division(id,leagueid,division)  values(?,?,?)";
      pinsert = con.prepareStatement(sql);
              pinsert.setInt(1,jos_fastball_division.getId());
        pinsert.setInt(2,jos_fastball_division.getLeagueid());
        pinsert.setString(3,jos_fastball_division.getDivision());
      int filas= pinsert.executeUpdate();
      pinsert.close();
      con.close();
      return true;
   } catch (Exception e) {
      excepcion = e;
   } finally {
  }
  return false;
 }


/**
*
* Editar()
*/

public static boolean Editar(Jos_fastball_division jos_fastball_division ) {
  excepcion = null;
  try {
      esMensaje =false;
     if (!Conectar()) {
         return false;
      }
      PreparedStatement pupdate = null;
      String sql = "update jos_fastball_division set  id=?, leagueid=?, division=?  where  id ='"+jos_fastball_division.getId()+"'  ";
      pupdate= con.prepareStatement(sql);
      pupdate.setInt(1,jos_fastball_division.getId());
pupdate.setInt(2,jos_fastball_division.getLeagueid());
pupdate.setString(3,jos_fastball_division.getDivision());
      int filas= pupdate.executeUpdate();
      pupdate.close();
      con.close();
      if (filas <= 0){
          excepcion = new Exception("No se pudo actualizar");
          esMensaje =true;
          return false;
      }
      return true;
   } catch (Exception e) {
      excepcion = e;
   } finally {
  }
  return false;
}


/**
*
* Editar()
*/

public static boolean Editar(String sqlupdate ) {
  excepcion = null;
  try {
      esMensaje =false;
     if (!Conectar()) {
         return false;
      }
      PreparedStatement pupdate = null;
      String sql = "update  jos_fastball_division set " + sqlupdate +"";
      pupdate= con.prepareStatement(sql);
      int filas= pupdate.executeUpdate();
      pupdate.close();
      con.close();
      if (filas <= 0){
          excepcion = new Exception("No se pudo editar");
           esMensaje =true;
          return false;
      }
      return true;
   } catch (Exception e) {
      excepcion = e;
   } finally {
  }
  return false;
}


/**
*
* EditarClavePrimaria()
*/

public static boolean EditarClavePrimaria(Jos_fastball_division jos_fastball_division_old , Jos_fastball_division jos_fastball_division) {
  excepcion = null;
  try {
      esMensaje =false;
     if (!Conectar()) {
         return false;
      }
      PreparedStatement pupdate = null;
      String sql = "update jos_fastball_division set  id=?  where  id ='"+jos_fastball_division_old.getId()+"'  ";
      pupdate= con.prepareStatement(sql);
      pupdate.setInt(1,jos_fastball_division.getId());
      int filas= pupdate.executeUpdate();
      pupdate.close();
      con.close();
      if (filas <= 0){
          excepcion = new Exception("No se pudo actualizar");
          esMensaje =true;
          return false;
      }
      return true;
   } catch (Exception e) {
      excepcion = e;
   } finally {
  }
  return false;
}


/**
*
* Eliminar()
*/

public static boolean Eliminar(Jos_fastball_division jos_fastball_division ) {
  excepcion = null;
  try {
      esMensaje =false;
     if (!Conectar()) {
         return false;
      }
      PreparedStatement pdelete = null;
      String sql = "delete from jos_fastball_division  where  id = ?  ";
      pdelete= con.prepareStatement(sql);
      pdelete.setInt(1,jos_fastball_division.getId());
      int filas= pdelete.executeUpdate();
      pdelete.close();
      con.close();
      if (filas <= 0){
          excepcion = new Exception("No se pudo eliminar");
           esMensaje =true;
          return false;
      }
      return true;
   } catch (Exception e) {
      excepcion = e;
   } finally {
  }
  return false;
}


/**
*
* EliminarSQL()
*/

public static boolean Eliminar(String sqldelete ) {
  excepcion = null;
  try {
      esMensaje =false;
     if (!Conectar()) {
         return false;
      }
      PreparedStatement pdelete = null;
      String sql = "delete from jos_fastball_division where " + sqldelete +"";
      pdelete= con.prepareStatement(sql);
      int filas= pdelete.executeUpdate();
      pdelete.close();
      con.close();
      if (filas <= 0){
          excepcion = new Exception("No se pudo eliminar");
           esMensaje =true;
          return false;
      }
      return true;
   } catch (Exception e) {
      excepcion = e;
   } finally {
  }
  return false;
}


/**
*
* ElimninarTodos()
*/

public static boolean EliminarTodos() {
  excepcion = null;
  try {
      esMensaje =false;
     if (!Conectar()) {
         return false;
      }
      PreparedStatement pdelete = null;
      String sql = "delete from jos_fastball_division  ";
      pdelete= con.prepareStatement(sql);
      int filas= pdelete.executeUpdate();
      pdelete.close();
      con.close();
      if (filas <= 0){
          excepcion = new Exception("No se pudo eliminar todos los registros");
           esMensaje =true;
          return false;
      }
      return true;
   } catch (Exception e) {
      excepcion = e;
   } finally {
  }
  return false;
}


/**
*
* ExisteRegistro()
*/



public static boolean ExisteRegistro(Jos_fastball_division jos_fastball_division) {
  excepcion = null;
  try {
      esMensaje =false;
      if (!Conectar()) {
         return false;
      }
      String sql = "select * from jos_fastball_division where id='"+jos_fastball_division.getId()+"'";
  PreparedStatement psquery = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
   ResultSet rs = psquery.executeQuery();
   rs.next();
   int x = rs.getRow();
   if (x == 0) {
      excepcion = new Exception("No existen registros en la tabla jos_fastball_divisioncon esa condicion");
       esMensaje =true;
      rs.close();
      psquery.close();
      con.close();
      return false;
   }
    rs.close();
    psquery.close();
    con.close();
 return true;
} catch (Exception ex) {
      excepcion = ex;
}
  return false;
}


/**
*
* findByPrimaryKey()
*/



public static Jos_fastball_division findByPrimaryKey(Jos_fastball_division jos_fastball_division) {
  excepcion = null;
  try {
      esMensaje =false;
      if (!Conectar()) {
         return null;
      }
      String sql = "select * from jos_fastball_division where id='"+jos_fastball_division.getId()+"'";
  PreparedStatement psquery = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
   ResultSet rs = psquery.executeQuery();
   rs.next();
   int x = rs.getRow();
   if (x == 0) {
      excepcion = new Exception("No existen registros en la tabla jos_fastball_divisioncon esa condicion");
       esMensaje =true;
      rs.close();
      psquery.close();
      con.close();
      return null;
   }
   rs.beforeFirst();
   while (rs.next()) {
   
            
            
            jos_fastball_division.setId(rs.getInt("id"));
jos_fastball_division.setLeagueid(rs.getInt("leagueid"));
jos_fastball_division.setDivision(rs.getString("division"));
    }
    rs.close();
    psquery.close();
    con.close();
 return jos_fastball_division;
} catch (Exception ex) {
      excepcion = ex;
}
  return null;
}


/**
*
* findById()
*/

public static List<Jos_fastball_division> findById(Jos_fastball_division jos_fastball_division) {
  excepcion = null;
  try {
      esMensaje =false;
      if (!Conectar()) {
         return null;
      }
      List<Jos_fastball_division> list = new ArrayList<>();
      String sql = "select * from jos_fastball_division where id='"+jos_fastball_division.getId()+"'";
  PreparedStatement psquery = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
   ResultSet rs = psquery.executeQuery();
   rs.next();
   int x = rs.getRow();
   if (x == 0) {
      excepcion = new Exception("No existen registros en la tabla jos_fastball_divisioncon esa condicion");
       esMensaje =true;
      rs.close();
      psquery.close();
      con.close();
      return null;
   }
   rs.beforeFirst();
   while (rs.next()) {
   
        Jos_fastball_division jos_fastball_division2 = new Jos_fastball_division(
         rs.getInt("id"),rs.getInt("leagueid"),rs.getString("division"));
            
            
       list.add(jos_fastball_division2);
    }
    rs.close();
    psquery.close();
    con.close();
    return list;
} catch (Exception ex) {
      excepcion = ex;
}
  return null;
}


/**
*
* findByLeagueid()
*/

public static List<Jos_fastball_division> findByLeagueid(Jos_fastball_division jos_fastball_division) {
  excepcion = null;
  try {
      esMensaje =false;
      if (!Conectar()) {
         return null;
      }
      List<Jos_fastball_division> list = new ArrayList<>();
      String sql = "select * from jos_fastball_division where leagueid='"+jos_fastball_division.getLeagueid()+"'";
  PreparedStatement psquery = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
   ResultSet rs = psquery.executeQuery();
   rs.next();
   int x = rs.getRow();
   if (x == 0) {
      excepcion = new Exception("No existen registros en la tabla jos_fastball_divisioncon esa condicion");
       esMensaje =true;
      rs.close();
      psquery.close();
      con.close();
      return null;
   }
   rs.beforeFirst();
   while (rs.next()) {
   
        Jos_fastball_division jos_fastball_division2 = new Jos_fastball_division(
         rs.getInt("id"),rs.getInt("leagueid"),rs.getString("division"));
            
            
       list.add(jos_fastball_division2);
    }
    rs.close();
    psquery.close();
    con.close();
    return list;
} catch (Exception ex) {
      excepcion = ex;
}
  return null;
}


/**
*
* findByDivision()
*/

public static List<Jos_fastball_division> findByDivision(Jos_fastball_division jos_fastball_division) {
  excepcion = null;
  try {
      esMensaje =false;
      if (!Conectar()) {
         return null;
      }
      List<Jos_fastball_division> list = new ArrayList<>();
      String sql = "select * from jos_fastball_division where division='"+jos_fastball_division.getDivision()+"'";
  PreparedStatement psquery = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
   ResultSet rs = psquery.executeQuery();
   rs.next();
   int x = rs.getRow();
   if (x == 0) {
      excepcion = new Exception("No existen registros en la tabla jos_fastball_divisioncon esa condicion");
       esMensaje =true;
      rs.close();
      psquery.close();
      con.close();
      return null;
   }
   rs.beforeFirst();
   while (rs.next()) {
   
        Jos_fastball_division jos_fastball_division2 = new Jos_fastball_division(
         rs.getInt("id"),rs.getInt("leagueid"),rs.getString("division"));
            
            
       list.add(jos_fastball_division2);
    }
    rs.close();
    psquery.close();
    con.close();
    return list;
} catch (Exception ex) {
      excepcion = ex;
}
  return null;
}


/**
*
* findByMapa()
*/

public static List<Jos_fastball_division> findByMapa(Map parametros) {
  excepcion = null;
  try {
      esMensaje =false;
      if (!Conectar()) {
         return null;
      }
  List<Jos_fastball_division> list = new ArrayList<>();
  String sql = "select * from jos_fastball_division";
  if (parametros != null) {
      Iterator it = parametros.entrySet().iterator();
      StringBuilder sb = new StringBuilder("");
      int i = 0;
      while (it.hasNext()) {
          if (i != 0){
            sb.append(" and ");
         }
         i++;
        Map.Entry e = (Map.Entry) it.next();
        sb.append(e.getKey() +"= '"+  e.getValue()+"'");
      }
      if (!sb.toString().equals("")){
         sql = sql + " where " + sb.toString();
      }
   }
  PreparedStatement psquery = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
   ResultSet rs = psquery.executeQuery();
   rs.next();
   int x = rs.getRow();
   if (x == 0) {
      excepcion = new Exception("No existen registros en la tabla jos_fastball_division con esa condición");
      esMensaje =true;
      rs.close();
      psquery.close();
      con.close();
      return null;
   }
   rs.beforeFirst();
   while (rs.next()) {
   
        Jos_fastball_division jos_fastball_division2 = new Jos_fastball_division(
         rs.getInt("id"),rs.getInt("leagueid"),rs.getString("division"));
            
            
       list.add(jos_fastball_division2);
    }
    rs.close();
    psquery.close();
    con.close();
    return list;
} catch (Exception ex) {
      excepcion = ex;
}
  return null;
}


/**
*
* Double Func(String funcion, String campo)
*/



public static Double Func(String funcion, String campo) {
  excepcion = null;
  try {
      esMensaje =false;
      if (!Conectar()) {
         return 0.0;
      }
      String sql = "select "+funcion+"("+campo+") as contador from jos_fastball_division";
  PreparedStatement psquery = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
   ResultSet rs = psquery.executeQuery();
   rs.next();
   int x = rs.getRow();
   if (x == 0) {
      excepcion = new Exception("No existen registros en la tabla jos_fastball_division ");
       esMensaje =true;
      rs.close();
      psquery.close();
      con.close();
      return 0.0;
   }
    Double tmp =0.0;
   rs.beforeFirst();
   while (rs.next()) {
    tmp =  rs.getDouble("contador");
    }
    rs.close();
    psquery.close();
    con.close();
 return tmp;
} catch (Exception ex) {
      excepcion = ex;
}
  return 0.0;
}


/**
*
* Double Func(String funcion, String campo,String where)
*/



public static Double Func(String funcion, String campo, String where) {
  excepcion = null;
  try {
      esMensaje =false;
      if (!Conectar()) {
         return 0.0;
      }
      String sql = "select "+funcion+"("+campo+") as contador from jos_fastball_division where "+where +" ";
  PreparedStatement psquery = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
   ResultSet rs = psquery.executeQuery();
   rs.next();
   int x = rs.getRow();
   if (x == 0) {
      excepcion = new Exception("No existen registros en la tabla jos_fastball_division ");
       esMensaje =true;
      rs.close();
      psquery.close();
      con.close();
      return 0.0;
   }
    Double tmp =0.0;
   rs.beforeFirst();
   while (rs.next()) {
    tmp =  rs.getDouble("contador");
    }
    rs.close();
    psquery.close();
    con.close();
 return tmp;
} catch (Exception ex) {
      excepcion = ex;
}
  return 0.0;
}


/**
*
* Contador()
*/



public static Integer Contador() {
  excepcion = null;
  try {
      esMensaje =false;
      if (!Conectar()) {
         return 0;
      }
      String sql = "select count(*) as contador from jos_fastball_division";
  PreparedStatement psquery = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
   ResultSet rs = psquery.executeQuery();
   rs.next();
   int x = rs.getRow();
   if (x == 0) {
      excepcion = new Exception("No existen registros en la tabla jos_fastball_division ");
       esMensaje =true;
      rs.close();
      psquery.close();
      con.close();
      return 0;
   }
   rs.beforeFirst();
   while (rs.next()) {
    x =  rs.getInt("contador");
    }
    rs.close();
    psquery.close();
    con.close();
 return x;
} catch (Exception ex) {
      excepcion = ex;
}
  return 0;
}


/**
*
* Contador(String sqlquery)
*/



public static Integer Contador(String sqlquery) {
  excepcion = null;
  try {
      esMensaje =false;
      if (!Conectar()) {
         return 0;
      }
      String sql = "select count(*) as contador from jos_fastball_division where  " + sqlquery +"";
  PreparedStatement psquery = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
   ResultSet rs = psquery.executeQuery();
   rs.next();
   int x = rs.getRow();
   if (x == 0) {
      excepcion = new Exception("No existen registros en la tabla jos_fastball_division ");
       esMensaje =true;
      rs.close();
      psquery.close();
      con.close();
      return 0;
   }
   rs.beforeFirst();
   while (rs.next()) {
    x =  rs.getInt("contador");
    }
    rs.close();
    psquery.close();
    con.close();
 return x;
} catch (Exception ex) {
      excepcion = ex;
}
  return 0;
}


/**
*
* Query()
*/

public static List<Jos_fastball_division> Query(String query) {
  excepcion = null;
  try {
      esMensaje =false;
      if (!Conectar()) {
         return null;
      }
  List<Jos_fastball_division> list = new ArrayList<>();
  String sql = "select * from jos_fastball_division where " + query +"";
  if (query == null || query.equals("")) {
  return null;
   }
  PreparedStatement psquery = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
   ResultSet rs = psquery.executeQuery();
   rs.next();
   int x = rs.getRow();
   if (x == 0) {
      excepcion = new Exception("No existen registros en la tabla jos_fastball_division con esa condición");
      esMensaje =true;
      rs.close();
      psquery.close();
      con.close();
      return null;
   }
   rs.beforeFirst();
   while (rs.next()) {
   
        Jos_fastball_division jos_fastball_division2 = new Jos_fastball_division(
         rs.getInt("id"),rs.getInt("leagueid"),rs.getString("division"));
            
            
       list.add(jos_fastball_division2);
    }
    rs.close();
    psquery.close();
    con.close();
    return list;
} catch (Exception ex) {
      excepcion = ex;
}
  return null;
}


/**
*
* Listar()
*/

public static List<Jos_fastball_division> getLista(Map parametros) {
  excepcion = null;
  try {
      esMensaje =false;
      if (!Conectar()) {
         return null;
      }
  List<Jos_fastball_division> list = new ArrayList<>();
  String sql = "select * from jos_fastball_division";
  if (parametros != null) {
      Iterator it = parametros.entrySet().iterator();
      StringBuilder sb = new StringBuilder("");
      while (it.hasNext()) {
        Map.Entry e = (Map.Entry) it.next();
        sb.append(e.getKey() +"="+  e.getValue());
      }
      if (!sb.toString().equals("")){
         sql = sql + " where " + sb.toString();
      }
   }
  PreparedStatement psquery = con.prepareStatement(sql,ResultSet.TYPE_SCROLL_INSENSITIVE,
                                      ResultSet.CONCUR_UPDATABLE);
   ResultSet rs = psquery.executeQuery();
   rs.next();
   int x = rs.getRow();
   if (x == 0) {
      excepcion = new Exception("No existen registros en la tabla jos_fastball_division");
       esMensaje =true;
      rs.close();
      psquery.close();
      con.close();
      return null;
   }
   rs.beforeFirst();
   while (rs.next()) {
       
        Jos_fastball_division jos_fastball_division2 = new Jos_fastball_division(
         rs.getInt("id"),rs.getInt("leagueid"),rs.getString("division"));
            
            
       list.add(jos_fastball_division2);
    }
    rs.close();
    psquery.close();
    con.close();
    return list;
} catch (Exception ex) {
      excepcion = ex;
}
  return null;
 }


/**
*
* Conectar()
*/



public static boolean Conectar() {
try {
 Sesion sesion = new Sesion();
  con = sesion.Conectar();
  if (con == null){
      return false;
   }
  return true;
  } catch (Exception ex) {
Mensajeria.MensajeError(ex, "Conectar()");
 }
    return false;
 }


}
